﻿using CPPEI.Coolzon.Core;
using CPPEI.Coolzon.Core.Repositories;
using CPPEI.Coolzon.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CPPEI.Coolzon.ManagementCenter
{
    public partial class SolutionList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            using (DbContext context = new DbContext())
            {
                ISolutionRepository solutionRepository = RepositoryFactory.Create<ISolutionRepository>();

                PagedList<CPPEI.Coolzon.Core.Data.Solution> privilegeList = solutionRepository.GetSolutionList(context, 0, int.MaxValue);
                this.gvSolutionList.DataSource = privilegeList.DataList;
                gvSolutionList.DataBind();
            }
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddSolution.aspx", false);
        }

        protected void gvSolutionList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string Id = ((Label)this.gvSolutionList.Rows[e.NewEditIndex].FindControl("lblId")).Text.Trim();
            Response.Redirect("EditSolution.aspx?Id=" + Id, false);
        }

        protected void gvSolutionList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SolutionService solutionService = new SolutionService();
            string Id = ((Label)this.gvSolutionList.Rows[e.RowIndex].FindControl("lblId")).Text.Trim();
            if (solutionService.RemoveSolution(Id).IsSuccess)
                LoadData();
        }
    }
}